package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.file.service.impl.FileStorageServiceImpl;
import com.zhentao.mapper.WmMaterialMapper;
import com.zhentao.model.common.dtos.PageResponseResult;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.model.wemedia.dto.WmMaterialDto;
import com.zhentao.model.wemedia.pojo.WmMaterial;
import com.zhentao.service.WmMaterialService;
import com.zhentao.utils.common.WmUserThreadLocalUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

@Service
public class WmMaterialServiceImpl implements WmMaterialService {
    @Autowired
    private FileStorageServiceImpl fileStorageService;
    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @SneakyThrows
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        //1.校验参数
        if (multipartFile == null || multipartFile.getSize() <= 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.把文件上传到minio里面
        String url = fileStorageService.uploadImageFile(multipartFile.getOriginalFilename(), multipartFile.getInputStream());

        //3.把数据保持到数据库里面
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setUserId(WmUserThreadLocalUtil.getUser().getId());
        //图片类型   可以在后台判断出来
        wmMaterial.setType((short) 0);
        wmMaterial.setUrl((url));

        wmMaterialMapper.insert(wmMaterial);

        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        //1.检查参数
        dto.checkParam();
        //2.分页查询
        IPage page = new Page(dto.getPage(), dto.getSize());

        QueryWrapper<WmMaterial> queryWrapper = new QueryWrapper<>();
        //根据用户Id查询
        queryWrapper.lambda().eq(WmMaterial::getUserId, WmUserThreadLocalUtil.getUser().getId());
        //排序    应该根据id来排序    能不能根据创建时间来排序  ？
        //  修改时间  1  2   3
        queryWrapper.orderByDesc("id");
        //是否收藏
        queryWrapper.lambda().eq(dto.getIsCollection() != null, WmMaterial::getIsCollection, dto.getIsCollection());

        //执行查询操作
        page = wmMaterialMapper.selectPage(page, queryWrapper);

        //3返回结果
        PageResponseResult pageResponseResult=new PageResponseResult();
        pageResponseResult.setSize(Integer.valueOf(page.getSize()+""));
        pageResponseResult.setTotal(Integer.valueOf(page.getTotal()+""));
        pageResponseResult.setCurrentPage(Integer.valueOf(page.getCurrent()+""));
        pageResponseResult.setData(page.getRecords());
        pageResponseResult.setCode(200);
        pageResponseResult.setErrorMessage("查询成功");

        return pageResponseResult;
    }
}
